package chexuan.game.util;

import chexuan.game.exception.NotRegisteredServiceException;
import chexuan.game.interfaces.context.InterfaceChexuanContext;
import chexuan.game.interfaces.service.EnumService;
import chexuan.game.interfaces.service.InterfaceConfigService;
import chexuan.game.interfaces.service.InterfaceEventService;
import chexuan.game.interfaces.service.InterfaceIOService;
import chexuan.game.interfaces.service.InterfaceService;
import chexuan.game.interfaces.service.InterfaceTask;
import chexuan.game.interfaces.service.InterfaceTaskService;
import chexuan.game.system.ChexuanContext;

public class SystemUtil {

	public static InterfaceChexuanContext getContext() {
		return ChexuanContext.getContext();
	}

	public static InterfaceIOService getIoService()
			throws NotRegisteredServiceException {
		InterfaceService service = getContext().getService(
				EnumService.IO_SERVICE);

		if (service instanceof InterfaceIOService) {
			return (InterfaceIOService) service;
		}

		return null;
	}

	public static InterfaceTaskService getTaskService()
			throws NotRegisteredServiceException {
		InterfaceService service = getContext().getService(
				EnumService.TASK_SERVICE);

		if (service instanceof InterfaceTaskService) {
			return (InterfaceTaskService) service;
		}

		return null;
	}

	public static void sumbmitTask(InterfaceTask task) {
		if (null == task) {
			return;
		}
		try {
			InterfaceTaskService taskService = getTaskService();

			if (null != taskService) {
				taskService.submit(task);
			}
		} catch (NotRegisteredServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static InterfaceEventService getEventService() {
		try {
			InterfaceService service = getContext().getService(
					EnumService.TASK_SERVICE);
			if (service instanceof InterfaceEventService) {
				return (InterfaceEventService) service;
			}

		} catch (NotRegisteredServiceException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static InterfaceConfigService getConfigService() {
		try {
			InterfaceService service = getContext().getService(
					EnumService.CONFIG_SERVICE);
			if (service instanceof InterfaceConfigService) {
				return (InterfaceConfigService) service;
			}

		} catch (NotRegisteredServiceException e) {
			e.printStackTrace();
		}

		return null;
	}
}
